<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------


use app\admin\model\Base;
use app\index\model\DealerModel;
// 应用公共文件
/**
 * 加密函数
 * @param $txt 待加密字段
 * @param string $key 公钥
 * @return string
 */
function lock_url($txt,$key='ycgzs')
{
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
    $nh = rand(0,64);
    $ch = $chars[$nh];
    $mdKey = md5($key.$ch);
    $mdKey = substr($mdKey,$nh%8, $nh%8+7);
    $txt = base64_encode($txt);
    $tmp = '';
    $i=0;$j=0;$k = 0;
    for ($i=0; $i<strlen($txt); $i++) {
        $k = $k == strlen($mdKey) ? 0 : $k;
        $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
        $tmp .= $chars[$j];
    }
    return urlencode($ch.$tmp);
}

/**
 * 解密函数
 * @param $txt 加密字段
 * @param string $key 公钥
 * @return bool|string
 */
function unlock_url($txt,$key='ycgzs')
{
    $txt = urldecode($txt);
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
    $ch = $txt[0];
    $nh = strpos($chars,$ch);
    $mdKey = md5($key.$ch);
    $mdKey = substr($mdKey,$nh%8, $nh%8+7);
    $txt = substr($txt,1);
    $tmp = '';
    $i=0;$j=0; $k = 0;
    for ($i=0; $i<strlen($txt); $i++) {
        $k = $k == strlen($mdKey) ? 0 : $k;
        $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
        while ($j<0) $j+=64;
        $tmp .= $chars[$j];
    }
    return base64_decode($tmp);
}

/**
 * 生成token
 * @param $userid 用戶ID
 * @param $time 登錄時間
 * @param $expiration 過期值
 * @return string 加密後的token
 */
function makeToken($userid,$time,$expiration){
    $token_data = [
        "userid" => $userid,
        "time" => $time,
        "expiration" => $expiration
    ];
    return lock_url(implode(",",$token_data));
}

/**
 * token驗證
 * @param $token token
 * @return mixed 驗證結果
 */
function tokenVerification($token){
    $msg = explode(",",unlock_url($token));
    $time = Base::selectLoginTime($msg[0]);
    if ($msg[1] != $time){
        $content["code"] = 401;
        $content["msg"] = "用户已在另一设备登录！";
        return $content;
    }else if ((intval($msg[1]) + intval($msg[2])) < time()){
        $content["code"] = 401;
        $content["msg"] = "token过期！";
        return $content;
    }else{
        $content["code"] = 200;
        return $content;
    }
}

/**
 * 判斷Token是否存在
 * @param $pram
 * @param $tokenPram
 * @return mixed
 */
function isToken($pram,$tokenPram){
    if (!array_key_exists("token",$pram) ||
        $tokenPram == null ||
        $tokenPram == "" || $tokenPram == "undefined"){
        $content["code"] = 401;
        $content["msg"] = "缺少token！";
        return $content;
    }else{
        $content["code"] = 200;
        return $content;
    }
}

/**
 * 驗證經銷商
 * @param $de_openid 經銷商微信openId
 * @return mixed
 */
function dealerVerification($de_openid){
    $msg = DealerModel::login($de_openid);
    if ($msg["code"] != 200){
        $result["code"] = 400;
        $result["msg"] = "經銷商不存在！";
        return $result;
    }
    if ($msg["de_type"] == 1){
        $result["code"] = 400;
        $result["msg"] = "經銷商被鎖定！";
        return $result;
    }
}